package com.atguigu.gulimall.order.queue;


import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnApplyEntity;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@Slf4j
@RabbitListener(queues = {"hello-java"})
public class MyCustomeQueue {

    @RabbitHandler
    public void receivedMessage(Message message,
                                OrderReturnApplyEntity content,
                                Channel channel){
        log.info("接收到的内容{} ",content);
        byte[] body = message.getBody();
        //消息头的属性信息
        MessageProperties messageProperties = message.getMessageProperties();
        log.info("消息处理完成  ==>",content.getCompanyAddress());
        //消费标签，通道内自增的
        long deliveryTag = messageProperties.getDeliveryTag();
        //消息确认
        log.info("deliveryTag  ==>{}",deliveryTag);

        /**
         *  签收消息()
         *  void basicAck(long deliveryTag, boolean multiple) throws IOException;
         *  deliveryTag  签收的标签，从通道中获取，默认是自增的
         *  multiple 是否是批量签收
         */
        try {
            if(deliveryTag%2 ==0){
                channel.basicAck(deliveryTag,false);
                log.info("签收了货物。。。{}"+deliveryTag);
            }else {
                /**
                 * 退货
                 *  long deliveryTag,
                 *  boolean multiple,
                 *  boolean requeue requeue = false丢弃 requeue = true丢弃 发回服务器，服务器重新入列
                 */
                channel.basicNack(deliveryTag,false,false);
                log.info("没有签收货物。。。{}"+deliveryTag);
            }
        } catch (Exception e) {
            //网络中断
        }
    }


    @RabbitHandler
    public void receivedMessage(Message message, OrderEntity content){
        log.info("接收到的内容{} ",content);
    }




}

